package com.demo.villageresidence.service.impl;

import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.villageresidence.dto.AdminDTO;
import com.demo.villageresidence.dto.Result;
import com.demo.villageresidence.pojo.Admin;
import com.demo.villageresidence.pojo.User;
import com.demo.villageresidence.service.AdminService;
import com.demo.villageresidence.mapper.AdminMapper;
import com.demo.villageresidence.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin>
        implements AdminService{


    @Override
    public Map<String,Object> login(AdminDTO adminDTO) {
        Map<String,Object> result = new HashMap<>();
        // 实现登录功能
        Admin admin = query().eq("admin_phone", adminDTO.getAdminPhone()).one();
        if (!BCrypt.checkpw(adminDTO.getAdminPassword(),admin.getAdminPassword().toString())) {
            result.put("code", 400);
            result.put("msg", "用户名或密码错误");
            result.put("token", null);
        }else{
            // 数据库中查询到有效信息生成token
            String token = JwtUtil.createToken();
            result.put("code", 200);
            result.put("msg", "登录成功");
            result.put("token", token);
            result.put("adminId", admin.getAdminId());
            result.put("adminName", admin.getAdminName());
        }
        return result;
    }

    @Override
    public Result getAllAdmin() {
        List<Admin> adminList = query().list();
        return Result.ok(adminList);
    }


}




